package com.yuexunit.operationman.nbsy.module.OperationInventory.adapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.TextView;

import com.yuexunit.operationman.nbsy.R;
import com.yuexunit.operationman.nbsy.module.OperationInventory.bean.InventoryBean;
import com.yuexunit.operationman.nbsy.widget.CountDialog;
import com.yuexunit.operationman.nbsy.widget.NoCountDialog;

import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.Collection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by chenss on 2017/8/15 0015.
 */

public class GoodsInventoryAdapter extends KJAdapter<InventoryBean.ObjectEntity.CheckItemListEntity> {

    public GoodsInventoryAdapter(AbsListView view, Collection<InventoryBean.ObjectEntity.CheckItemListEntity> mDatas, int itemLayoutId) {
        super(view, mDatas, itemLayoutId);
    }

    @Override
    public void convert(AdapterHolder helper, final InventoryBean.ObjectEntity.CheckItemListEntity item, boolean isScrolling, final int position) {
        helper.setText(R.id.name,item.getItemName());
        final TextView subtract, add;
        subtract = helper.getView(R.id.subtract);
        add = helper.getView(R.id.add);
        subtract.setVisibility(View.GONE);
        add.setVisibility(View.GONE);
        final TextView num_01, num_02, num_03, num_04, num_05, num_06;
        num_01 = helper.getView(R.id.num_01);
        num_02 = helper.getView(R.id.num_02);
        num_03 = helper.getView(R.id.num_03);
        num_04 = helper.getView(R.id.num_04);
        num_05 = helper.getView(R.id.num_05);
        num_06 = helper.getView(R.id.num_06);

        num_01.setText(item.getBeforeCheck() + "");
        num_04.setText(item.getBeforeCloseBody() + "");
        num_05.setText(item.getAfterCloseBody() + "");
        num_06.setText(item.getAfterSkin() + "");

        num_02.setText(item.getInAdd());

        Pattern pattern = Pattern.compile("[+-]\\d+");
        Matcher matcher = pattern.matcher(item.getInAdd());
        int all =0;
        while(matcher.find()) {
            if(matcher.group().startsWith("+"))
            {
                all+= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
            }
            else if(matcher.group().startsWith("-"))
            {
                all-= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
            }
        }

        num_03.setText((item.getBeforeCheck()+all)+"");

//        subtract.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                if(item.getBeforeCheck() > 0){
//                    num_01.setText((item.getBeforeCheck() - 1) + "");
//                    item.setBeforeCheck(item.getBeforeCheck() - 1);
//                    num_03.setText((item.getBeforeCheck()) + "");
//                }
//            }
//        });
//
//        add.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                int count=Integer.valueOf(num_01.getText().toString());
//                count++;
//                num_01.setText(count + "");
//                item.setBeforeCheck(count);
//                int total=Integer.valueOf(num_03.getText().toString());
//                total++;
//                num_03.setText(total + "");
//            }
//        });

        num_02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final CountDialog dialog = new CountDialog(mCxt).builder();
                Pattern pattern = Pattern.compile("[+-]\\d+");
                Matcher matcher = pattern.matcher(item.getInAdd());
                int all =0;
                while(matcher.find()) {
                    if(matcher.group().startsWith("+"))
                    {
                        all+= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
                    }
                    else if(matcher.group().startsWith("-"))
                    {
                        all-= Integer.parseInt(matcher.group().substring(1,matcher.group().length()));
                    }
                }

                dialog.setNum(item.getBeforeCheck()+all);
                dialog.setCallBack(new CountDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
                            int newNum = bundle.getInt("newNum");
                            if(newNum > 0){
                                num_02.setText(item.getInAdd() + "+" + newNum);
                                item.setInAdd(item.getInAdd() + "+" + newNum);
                            }else if(newNum < 0){
                                num_02.setText(item.getInAdd() + newNum);
                                item.setInAdd(item.getInAdd() + newNum);
                            }
                            int num = Integer.parseInt(num_03.getText().toString());
                            num_03.setText((num + newNum) + "");
                            num_04.setText((item.getBeforeCloseBody() + newNum) + "");
                            num_05.setText((item.getAfterCloseBody() + newNum) + "");
                            num_06.setText((item.getAfterSkin() + newNum) + "");
                            item.setBeforeCloseBody(item.getBeforeCloseBody() + newNum);
                            item.setAfterCloseBody(item.getAfterCloseBody() + newNum);
                            item.setAfterSkin(item.getAfterSkin() + newNum);
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        num_04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final NoCountDialog dialog = new NoCountDialog(mCxt).builder();
                dialog.setNum(item.getBeforeCloseBody());
                dialog.setCallBack(new NoCountDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
//                            int newNum = bundle.getInt("newNum");
                            num_04.setText(bundle.getString("newNum"));
//                            item.setBeforeCloseBody(item.getBeforeCloseBody() + newNum);
                            item.setBeforeCloseBody(Integer.valueOf(bundle.getString("newNum")));
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        num_05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final NoCountDialog dialog = new NoCountDialog(mCxt).builder();
                dialog.setNum(item.getAfterCloseBody());
                dialog.setCallBack(new NoCountDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
//                            int newNum = bundle.getInt("newNum");
                            num_05.setText(bundle.getString("newNum"));
//                            item.setAfterCloseBody(item.getAfterCloseBody() + newNum);
                            item.setAfterCloseBody(Integer.valueOf(bundle.getString("newNum")));
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
        num_06.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final NoCountDialog dialog = new NoCountDialog(mCxt).builder();
                dialog.setNum(item.getAfterSkin());
                dialog.setCallBack(new NoCountDialog.ICallBack() {
                    @Override
                    public void callback(Bundle bundle) {
                        if(bundle != null){
//                            int newNum = bundle.getInt("newNum");
                            num_06.setText(bundle.getString("newNum"));
//                            item.setAfterSkin(item.getAfterSkin() + newNum);
                            item.setAfterSkin(Integer.valueOf(bundle.getString("newNum")));
                        }
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
    }
}
